// pages/setSwitchTime/setSwitchTime.js
var app=getApp()
var selected_devNo=[]
Page({

  /**
   * 页面的初始数据
   */
  data: {
    poweron_time:'00:00',
    poweroff_time:'23:59',
    auto:false

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    selected_devNo = JSON.parse(options.selected_devNo);

  },

  bindpoweronChange:function(e){
    let that = this;
    that.setData({
      poweron_time:e.detail.value
    });  
  },

  bindpoweroffChange:function(e){   
    let that = this;
    that.setData({
      poweroff_time: e.detail.value
    });
  },

  set_auto:function(e){    
    let that = this;
    that.setData({
      auto:e.detail.value
    });    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  formSubmit: function (e) {
    let that = this;

    let header = {
      'Content-Type': 'application/x-www-form-urlencoded',
      'cookie': "openid=" + app.globalData.openid
    } 

    if (that.data.poweron_time == "") {
      wx.showModal({
        title: '提示',
        content: '开灯时间不能为空',
        showCancel: false
      });
      return;
    }

    if (that.data.poweroff_time == "") {
      wx.showModal({
        title: '提示',
        content: '控制器编号不能为空',
        showCancel: false
      });

      return;
    }   

    that.saveTimeSettings();  
  },

  formReset: function (e) {
    let that = this;

    let pages = getCurrentPages(); //获取上一个页面信息栈(a页面)
    let prevPage = pages[pages.length - 2] //给上一页面的longitude latitude赋值 
    wx.navigateBack(); //关闭当前页面，返回上一个页面    
  },

  saveTimeSettings:function()
  {
    let that = this;
    let devNo = selected_devNo.join(',') + "";
    let header = {
      'Content-Type': 'application/x-www-form-urlencoded',
      'cookie': "openid=" + app.globalData.openid
    } 
  
    if(that.data.auto) {
      wx.request({
        url: "https://119.3.46.145/server/setAutoTime",
        type: "post",
        header:header,
        data: {
          devNo: devNo
        },
        success: function (response) {         
          //console.log(response, "response");
          if(response.data.error == "0"){
            wx.showModal({
              title: '提示',
              content: '自动更新开关时间成功',
              showCancel:false,
              success:res=>{
                wx.navigateBack();
              }
            })
          }
          else{
            wx.showModal({
              title: '提示',
              content: '自动更新开关时间失败',
              showCancel: false
            })
          }
        }
      })
    }
    else{
      wx.request({
        url: "https://119.3.46.145/server/UpdateAutoTime",
        data: {
          devNo: devNo,
          ontime: that.data.poweron_time,
          offtime: that.data.poweroff_time,
          userid: app.globalData.userid
        },
        success: function (response) {
          //console.log(response, "obj");
          let obj = response.data;
      
          if (obj.error === "0") {
            // $("#timeSetDialog").window("close");
            // initSingleLightTable();
           wx.showModal({
             title:'提示',
             content:'批量修改时间成功',
             showCancel:false,
             success:res=>{
               wx.navigateBack();

             }
           });
          }
          else{
            wx.showModal({
              title: '提示',
              content: '批量修改时间失败',
              showCancel: false
            });
          }
        }
      })
    }
    // else {
    //   wx.showModal({
    //     title: '提示',
    //     content: '输入有误，请检查输入'
    //   });
    // }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})